//@author A0087191Y
package net.mysoc.w111j.service.actions;

import net.mysoc.w111j.model.BadCategoryNameException;
import net.mysoc.w111j.model.Category;
import net.mysoc.w111j.model.User;

/**
 * An {@code Action} to undo the renaming of a {@code Category}.
 */
public class RenameCategoryAction implements Action {
    private final User user;
    private final Category oldCategory;
    private String status;

    public RenameCategoryAction(User user, Category oldCategory) {
        this.user = user;
        this.oldCategory = new Category(oldCategory.getId(), oldCategory.getName());
        setStatus(oldCategory);
    }

    @Override
    public void undo() {
        try {
            user.updateCategory(oldCategory);
        } catch (BadCategoryNameException e) {
            // This is guaranteed to not happen here.
            throw new RuntimeException(e);
        }
    }

    private void setStatus(Category category) {
        status = "Renamed category: " + category.getName();
    }

    @Override
    public String getStatus() {
        return status;
    }
}
